<?php

return array(
    'service_manager' => array(
        'factories' => array(
            'Authentication\Form\LoginFilter' => 'Authentication\Form\LoginFilterFactory',
            'Authentication\Form\LoginForm' => 'Authentication\Form\LoginFormFactory',
            'Authentication\Service\AuthenticationStorage' => function ($sm) {
                $subStorage = new \Zend\Authentication\Storage\Session();
                $userRepository = $sm->get('User\Repository\UserRepository');
    
                // TODO: change storage to prevent deleted users from using session cached data!
                $storage = new \Authentication\Service\UserIdStorage();
                $storage->setStorage($subStorage);
                $storage->setUserRepository($userRepository);
                $storage->init(); // TODO: maybe there is a better way
                return $storage;
            },
            'Authentication\Service\AuthenticationAdapter' => function ($sm) {
                $userRepository = $sm->get('User\Repository\UserRepository');
                $authAdapter = new \Authentication\Service\UserRepositoryAdapter();
                $authAdapter->setUserRepository($userRepository);
                return $authAdapter;
            },
            'Authentication\Service\AuthenticationService' => function ($sm) {
                $storage = $sm->get('Authentication\Service\AuthenticationStorage');
                $authAdapter = $sm->get('Authentication\Service\AuthenticationAdapter');
                $service = new \Zend\Authentication\AuthenticationService($storage, $authAdapter);
                return $service;
            },
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Authentication\Controller\Authentication' => 'Authentication\Controller\AuthenticationController',
        ),
    ),
    'view_helpers' => array(
        'factories' => array(
            'auth' => function ($hpm) {
                $sl = $hpm->getServiceLocator();
                $viewHelper = new \Authentication\View\Helper\AuthViewHelper();
                $viewHelper->setAuthService($sl->get('Authentication\Service\AuthenticationService'));
                return $viewHelper;
            },
        )
    ),
    'router' => array(
        'routes' => array(
            'auth' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '/auth[/:action]',
                    'defaults' => array(
                        'controller' => 'Authentication\Controller\Authentication',
                    ),
                ),
            ),
        ),
    ),
    'authorization' => array(
        'Authentication\Controller\Authentication' => array(
            'login' => 'anonymous',
            'logout' => 'any',
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            'authentication' => __DIR__ . '/../view',
        ),
    ),
);